package psn.polaris.http;

import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.classic.methods.HttpPost;
import org.apache.hc.client5.http.entity.UrlEncodedFormEntity;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.core5.http.NameValuePair;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.apache.hc.core5.http.message.BasicHeader;
import org.apache.hc.core5.http.message.BasicNameValuePair;
import psn.polaris.http.callback.IHttpCallBack;
import psn.polaris.http.dto.HttpMethod;
import psn.polaris.http.dto.HttpRequestDTO;
import psn.polaris.util.SpringUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class HttpClientUtils {

    /**
     * 同步POST请求
     * @param requestDTO        请求信息
     */
    public static void syncPost(HttpRequestDTO requestDTO){
        if(!HttpMethod.POST.equals(requestDTO.getMethod())){
            throw new RuntimeException("请求方法不是POST");
        }
        if(!requestDTO.getSync().isSync()){
            throw new RuntimeException("请求方法不是同步");
        }
        int i = 1;
        for (String requestUrl : requestDTO.getRequestUrls()) {
            HttpPost post = new HttpPost(requestUrl);
            List<NameValuePair> requestParam = new ArrayList<>();
            // 组装请求信息
            for (Map.Entry<String, String> requestEntry : requestDTO.getRequestMap().entrySet()) {
                NameValuePair nameValuePair = new BasicNameValuePair(requestEntry.getKey(), requestEntry.getValue());
                requestParam.add(nameValuePair);
            }
            // 组装Head信息
            post.setEntity(new UrlEncodedFormEntity(requestParam));
            for (Map.Entry<String, String> headerEnrty : requestDTO.getHeader().entrySet()) {
                post.addHeader(new BasicHeader(headerEnrty.getKey(), headerEnrty.getValue()));
            }
            // 发起请求
            try(CloseableHttpClient httpClient = HttpClients.createDefault()){
                try(CloseableHttpResponse response = httpClient.execute(post)){
                    int code = response.getCode()/100;
                    if(2==code || 3==code) {
                        byte[] result = EntityUtils.toByteArray(response.getEntity());
                        EntityUtils.consume(response.getEntity());
                        requestDTO.getResultMap().put(i, result);
                    }
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            i++;
        }
        // 执行Http请求回调
        IHttpCallBack<?> callBack = SpringUtils.getSpringBeanByType(requestDTO.getRespBeanId(), IHttpCallBack.class);
        callBack.process(requestDTO);
    }

    /**
     * 同步GET请求
     * @param requestDTO        请求信息
     */
    public static void synvGet(HttpRequestDTO requestDTO){
        if(!HttpMethod.POST.equals(requestDTO.getMethod())){
            throw new RuntimeException("请求方法不是POST");
        }
        if(!requestDTO.getSync().isSync()){
            throw new RuntimeException("请求方法不是同步");
        }
        int i = 1;
        for (String requestUrl : requestDTO.getRequestUrls()) {
            HttpGet post = new HttpGet(requestUrl);
            List<NameValuePair> requestParam = new ArrayList<>();
            // 组装请求信息
            for (Map.Entry<String, String> requestEntry : requestDTO.getRequestMap().entrySet()) {
                NameValuePair nameValuePair = new BasicNameValuePair(requestEntry.getKey(), requestEntry.getValue());
                requestParam.add(nameValuePair);
            }
            // 组装Head信息
            post.setEntity(new UrlEncodedFormEntity(requestParam));
            for (Map.Entry<String, String> headerEnrty : requestDTO.getHeader().entrySet()) {
                post.addHeader(new BasicHeader(headerEnrty.getKey(), headerEnrty.getValue()));
            }
            // 发起请求
            try(CloseableHttpClient httpClient = HttpClients.createDefault()){
                try(CloseableHttpResponse response = httpClient.execute(post)){
                    int code = response.getCode()/100;
                    if(2==code || 3==code) {
                        byte[] result = EntityUtils.toByteArray(response.getEntity());
                        EntityUtils.consume(response.getEntity());
                        requestDTO.getResultMap().put(i, result);
                    }
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            i++;
        }
        // 执行Http请求回调
        IHttpCallBack<?> callBack = SpringUtils.getSpringBeanByType(requestDTO.getRespBeanId(), IHttpCallBack.class);
        callBack.process(requestDTO);
    }

}
